package com.example.demo.mysort;

public class SortPractice {


    // 冒泡排序
    public void bubbleSorting(int[] nums){
        int len = nums.length;
        for (int i =0 ;i<len-1;i++){
            for (int j = i+1;j<len;j++){
                if(nums[j]<nums[i]){
                    int t = nums[i];
                    nums[i] = nums[j];
                    nums[j] = t;
                }
            }
        }
    }

    // 快速排序
    public void quickSort(int[] nums,int low,int high){
        if (low>high){
            return;
        }
        int i = low ,j = high;
        int temp =  nums[low];
        while (i<j){
            while (i<j && nums[j] >= temp) j--;
            while (i<j && nums[i] <=temp) i++;
            if (i<j){
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
            }
        }
        nums[low] = nums[i];
        nums[i] = temp;
        quickSort(nums,low,j-1);
        quickSort(nums,j+1,high);
    }

    // 插入排序
    public void insertSort(int[]nums){
        int len = nums.length;
        for (int i = 1;i<len;i++){
            int j = i-1;
            int temp = nums[i];
            for (;j>=0;j--){
                if (nums[j]>temp){
                    nums[j+1] = nums[j];
                }else {
                    break;
                }
            }
            nums[j+1] = temp;
        }
    }
}


class TestSort{

    public static void main(String[] args) {
        int[] nums = {1,7,2,4,5,3,9,6,8};
        SortPractice sp = new SortPractice();
//        sp.bubbleSorting(nums);
//        System.out.println("冒泡排序后的数组是:");
//        for (int i:nums) {
//            System.out.print(i+" ");
//        }
//        sp.quickSort(nums,0,nums.length-1);
//        System.out.println("快速排序后的数组是:");
//        for (int i:nums) {
//            System.out.print(i+" ");
//        }
        sp.insertSort(nums);
        System.out.println("插入排序后的数组是");
        for (int i:nums){
            System.out.print(i+" ");
        }
    }
}